package com.meichis.yndms.vm;

import android.content.Intent;
import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;

import com.amap.api.location.AMapLocation;
import com.meichis.mcsappframework.entity.Attachment;
import com.meichis.mcsappframework.utils.ActivityUtils;
import com.meichis.mcsappframework.utils.DateUtil;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;
import com.meichis.yndms.common.MCode;
import com.meichis.yndms.common.PictureUtils;
import com.meichis.yndms.model.*;
import com.meichis.yndms.repository.manager.VisitWorkItemJdManager;
import com.meichis.yndms.room.AppDatabase;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author xmy
 * @title
 * @remake
 * @create 2022-03-21 11:05
 */
public class VisitWorkItemJdVM extends MyViewModel {

    public ClientInfo RT;
    public VisitWork work;
    public List<Picture> piclist = new ArrayList<>();
    public List<Attachment> attlist = new ArrayList<>();
    public VisitTemplate RTtemplate;
    public String code = "";
    public String workGuid = "";
    public String remark = "";
    public int distance = 0;
    public AMapLocation location;
    private MutableLiveData<String> nowTime;
    private MutableLiveData<Integer> workID;
    private MutableLiveData<Boolean> jdComplate;
    public VisitWorkItem item;
    public String address;
    public boolean needNewLocation = true;

    public MutableLiveData<Boolean> getJdComplate() {
        if (jdComplate == null) {
            jdComplate = new MutableLiveData<>();
            jdComplate.setValue(false);
        }
        return jdComplate;
    }

    public MutableLiveData<Integer> getWorkID() {
        if (workID == null) {
            workID = new MutableLiveData<>();
        }
        return workID;
    }

    public MutableLiveData<String> getNowTime() {
        if (nowTime == null) {
            nowTime = new MutableLiveData<>();
        }
        return nowTime;
    }

    public void setIntentData(Intent intentData) {
        RT = (ClientInfo) intentData.getSerializableExtra(MCode.CLIENT);
        RTtemplate = (VisitTemplate) intentData.getSerializableExtra("VisitTemplate");
        code = intentData.getStringExtra(MCode.CODE);
        workGuid = intentData.getStringExtra(MCode.VISITGUID);
        if (!TextUtils.isEmpty(workGuid)) {
            work = AppDatabase.getInstance().visitWorkDao().find(workGuid);
        } else {
            workGuid = UUID.randomUUID().toString();
        }
        item = AppDatabase.getInstance().visitWorkItemDao().find(workGuid, code);
        if (item != null) {
            attlist = item.getAtts();
            List<Picture> pics = PictureUtils.Attachment2Picture(attlist);
            piclist.clear();
            piclist.addAll(pics);
        }
    }

    public void beginWork() {
        work = new VisitWork();
        work.setClient(RT.getID());
        work.setClientInfo(RT);
        work.setGUID(workGuid);
        work.setClientName(RT.getFullName());
        work.setVisitTemplate(RTtemplate.getID());
        work.setVisitTemplateName(RTtemplate.getName());
        work.setBeginTime(DateUtil.getCurDateStr(DateUtil.FORMAT_YMDHMS));

        float[] result = new float[1];
        AMapLocation.distanceBetween(RT.getLatitude(), RT.getLongitude(), location.getLatitude(), location.getLongitude(), result);
        distance = Math.round(result[0]);
        work.setGeoOffset(String.valueOf(distance));

        AppDatabase.getInstance().visitWorkDao().insert(work);
    }

    public void save() {
        needNewLocation = false;

        if (work == null) {
            beginWork();
        }
        if (item == null) {
            item = new VisitWorkItem();
            item.setProcessCode(code);
            item.setWorkTime(DateUtil.getCurDateStr(DateUtil.FORMAT_YMDHMS));
            item.setVisitGUID(workGuid);
            item.setEndTime(DateUtil.getCurDateStr(DateUtil.FORMAT_YMDHMS));
            VisitWorkItem_JD jd = new VisitWorkItem_JD();
            switch (code) {
                case "JD":
                    jd.setJobType(1);
                    break;
                case "LD":
                    jd.setJobType(2);
                    break;
            }
            jd.setJudgeMode(1);
            if (location != null) {
                jd.setLatitude(location.getLatitude());
                jd.setLongitude(location.getLongitude());
                jd.setAddress(location.getAddress());
                float[] result = new float[1];
                AMapLocation.distanceBetween(RT.getLatitude(), RT.getLongitude(), location.getLatitude(), location.getLongitude(), result);
                remark = Math.round(result[0]) + "米";
                jd.setRemark(remark);

            }
            item.setJD(jd);
            item.setAtts(attlist);
            AppDatabase.getInstance().visitWorkItemDao().insert(item);
        } else {
            item.setEndTime(DateUtil.getCurDateStr(DateUtil.FORMAT_YMDHMS));
            item.setAtts(attlist);
            if (location != null) {
                item.getJD().setLatitude(location.getLatitude());
                item.getJD().setLongitude(location.getLongitude());
                item.getJD().setAddress(location.getAddress());
                float[] result = new float[1];
                AMapLocation.distanceBetween(RT.getLatitude(), RT.getLongitude(), location.getLatitude(), location.getLongitude(), result);
                remark = Math.round(result[0]) + "米";
                item.getJD().setRemark(remark);
            }
            AppDatabase.getInstance().visitWorkItemDao().update(item);
        }
        if (code.equals("JD")) {
            util.setObjectValue(SharePreferenceUtil.PREFERENCES_VSTLOCATION + RT.getID(), location == null ? "暂无" : address);
            util.setObjectValue(SharePreferenceUtil.PREFERENCES_VSTLOCATION, location == null ? "暂无" : address);
            getJdComplate().setValue(true);
        } else {
            EndWork();
        }
    }

    private void EndWork() {
        work.setEndTime(DateUtil.getCurDateStr(DateUtil.FORMAT_YMDHMS));
        List<VisitWorkItem> items = AppDatabase.getInstance().visitWorkItemDao().findAll(work.getGUID());
        work.getItems().clear();
        work.getItems().addAll(items);
        work.setIsComplete(true);
        AppDatabase.getInstance().visitWorkDao().update(work);

        util.remove("InventoryCheck" + work.getGUID());
        ActivityUtils.getInstance().FinishActivity("VST_TemplateActivity");
        VisitWorkAdd(work);
    }

    public void VisitWorkAdd(VisitWork work) {
        VisitWorkItemJdManager.getInstance().VisitWorkAdd(getShowDialog(), work, getWorkID());
    }
}
